// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

// ************************************************************************* //
//  File: avtTessellateFilter.C
// ************************************************************************* //

#include <avtTessellateFilter.h>

#include <vtkPointData.h>
#include <vtkTessellatorFilter.h>
#include <vtkUnstructuredGrid.h>

// ****************************************************************************
//  Method: avtTessellateFilter constructor
//
//  Programmer: brugger -- generated by xml2avt
//  Creation:   Mon Jul 20 16:18:00 PST 2020
//
// ****************************************************************************

avtTessellateFilter::avtTessellateFilter()
{
}


// ****************************************************************************
//  Method: avtTessellateFilter destructor
//
//  Programmer: brugger -- generated by xml2avt
//  Creation:   Mon Jul 20 16:18:00 PST 2020
//
//  Modifications:
//
// ****************************************************************************

avtTessellateFilter::~avtTessellateFilter()
{
}


// ****************************************************************************
//  Method:  avtTessellateFilter::Create
//
//  Programmer: brugger -- generated by xml2avt
//  Creation:   Mon Jul 20 16:18:00 PST 2020
//
// ****************************************************************************

avtFilter *
avtTessellateFilter::Create()
{
    return new avtTessellateFilter();
}


// ****************************************************************************
//  Method:      avtTessellateFilter::SetAtts
//
//  Purpose:
//      Sets the state of the filter based on the attribute object.
//
//  Arguments:
//      a        The attributes to use.
//
//  Programmer: brugger -- generated by xml2avt
//  Creation:   Mon Jul 20 16:18:00 PST 2020
//
// ****************************************************************************

void
avtTessellateFilter::SetAtts(const AttributeGroup *a)
{
    atts = *(const TessellateAttributes*)a;
}


// ****************************************************************************
//  Method: avtTessellateFilter::Equivalent
//
//  Purpose:
//      Returns true if creating a new avtTessellateFilter with the given
//      parameters would result in an equivalent avtTessellateFilter.
//
//  Programmer: brugger -- generated by xml2avt
//  Creation:   Mon Jul 20 16:18:00 PST 2020
//
// ****************************************************************************

bool
avtTessellateFilter::Equivalent(const AttributeGroup *a)
{
    return (atts == *(TessellateAttributes*)a);
}


// ****************************************************************************
//  Method: avtTessellateFilter::ExecuteData
//
//  Purpose:
//      Sends the specified input and output through the Tessellate filter.
//
//  Arguments:
//      in_dr      The input data representation.
//
//  Returns:       The output data representation.
//
//  Programmer: brugger -- generated by xml2avt
//  Creation:   Mon Jul 20 16:18:00 PST 2020
//
//  Modifications:
//    Eric Brugger, Sat Aug 22 07:05:39 PDT 2020
//    I added code to set the field criterion for the tessellate filter.
//
// ****************************************************************************

avtDataRepresentation *
avtTessellateFilter::ExecuteData(avtDataRepresentation *in_dr)
{
    //
    // Get the VTK data set, the domain number, and the label.
    //
    vtkDataSet *in_ds = in_dr->GetDataVTK();

    //
    // Use the vtkTessellatorFilter.
    //
    vtkTessellatorFilter *tessellator = vtkTessellatorFilter::New();

    tessellator->SetInputData(in_ds);
    tessellator->SetChordError(atts.GetChordError());
    for (int i = 0; i < in_ds->GetPointData()->GetNumberOfArrays(); i++)
        tessellator->SetFieldCriterion(i, atts.GetFieldCriterion());
    tessellator->SetMergePoints(atts.GetMergePoints());
    tessellator->Update();

    vtkUnstructuredGrid *output = tessellator->GetOutput();

    avtDataRepresentation *out_dr = new avtDataRepresentation(output,
        in_dr->GetDomain(), in_dr->GetLabel());

    tessellator->Delete();
    return out_dr;
}


